package com.burst.reptile.modular.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.burst.reptile.common.constant.UserConstants;
import com.burst.reptile.common.utils.poi.ExcelUtil;
import com.burst.reptile.framework.aspectj.lang.annotation.Log;
import com.burst.reptile.framework.aspectj.lang.enums.BusinessType;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.LayuiResult;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.system.entity.SysPost;
import com.burst.reptile.modular.system.service.ISysPostService;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author wujin
 * @since 2020-05-22
 */
@RestController
@RequestMapping("/system/post")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysPostController extends BaseController {

	private final ISysPostService postService;

	/**
	 * 获取岗位列表
	 */
	@PreAuthorize("@ss.hasPermi('system:post:list')")
	@GetMapping("/list")
	public LayuiResult<Object> list(SysPost post) {
		Page<SysPost> page = startPage();
		Page<SysPost> list = postService.selectPagePostList(page, post);
		return LayuiResult.success(list.getTotal(), list.getRecords());
	}

	/**
	 * 导出岗位数据
	 * @param post
	 * @return
	 */
	@Log(title = "岗位管理", businessType = BusinessType.EXPORT)
	@PreAuthorize("@ss.hasPermi('system:post:export')")
	@GetMapping("/export")
	public Result<Object> export(SysPost post) {
		List<SysPost> list = postService.selectPostList(post);
		ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
		return util.exportExcel(list, "岗位数据");
	}

	/**
	 * 新增岗位
	 */
	@PreAuthorize("@ss.hasPermi('system:post:add')")
	@Log(title = "岗位管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	public Result<Object> add(@Validated @RequestBody SysPost post) {
		if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
			return Result.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
		} else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
			return Result.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
		}
		return Result.success(postService.insertPost(post));
	}

	/**
	 * 修改岗位
	 */
	@PreAuthorize("@ss.hasPermi('system:post:edit')")
	@Log(title = "岗位管理", businessType = BusinessType.UPDATE)
	@PutMapping("/edit")
	public Result<Object> edit(@Validated @RequestBody SysPost post) {
		if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
			return Result.error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
		} else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
			return Result.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
		}
		return Result.success(postService.updatePost(post));
	}

	/**
	 * 删除岗位
	 */
	@PreAuthorize("@ss.hasPermi('system:post:remove')")
	@Log(title = "岗位管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/remove/{postIds}")
	public Result<Object> remove(@PathVariable Long[] postIds) {
		return Result.success(postService.deletePostByIds(postIds));
	}
	
	/**
	 * 状态修改
	 */
	@PreAuthorize("@ss.hasPermi('system:post:edit')")
	@Log(title = "岗位管理", businessType = BusinessType.UPDATE)
	@PutMapping("/changeStatus")
	public Result<Object> changeStatus(@RequestBody SysPost post) {
		return Result.success(postService.updatePostStatus(post));
	}


}
